package com.ztools.pc.service.impl.store;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.store.Commodity;
import com.ztools.common.model.pc.store.OnlineStock;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.ServiceException;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.store.OnlineStockMapper;
import com.ztools.pc.service.store.CommodityService;
import com.ztools.pc.service.store.OnlineStockService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2023/5/20  17:01
 * @Version 1.0
 */
@Service
public class OnlineStockServiceImpl extends ServiceImpl<OnlineStockMapper, OnlineStock> implements OnlineStockService {

    @Resource
    private DictService dictService;
    @Resource
    private CommodityService commodityService;

    @Override
    public Page<OnlineStock> findList(PageUtil<OnlineStock> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<OnlineStock> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<OnlineStock> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<OnlineStock> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<OnlineStock> findByObject(OnlineStock commodity) {
        QueryWrapper<OnlineStock> queryWrapper = new QueryWrapper<>();
        return list(queryWrapper);
    }

    @Override
    public List<OnlineStock> findBy(String fieldName, Object value) {
        QueryWrapper<OnlineStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(fieldName, value);
        return list(queryWrapper);
    }

    @Override
    public boolean delete(OnlineStock onlineStock) {
        boolean b = false;
        OnlineStock byId = getById(onlineStock);
        b = removeById(byId);
        return b;
    }

    @Override
    public boolean add(OnlineStock onlineStock) {
        return save(onlineStock);
    }

    @Override
    public boolean update(OnlineStock onlineStock) {
        boolean b = false;
        // 修改前先获取，在删除（乐观锁）
        OnlineStock byId = getById(onlineStock);
        b = updateById(onlineStock);
        return b;
    }


    @Override
    public List<OnlineStock> getStoreMall() {
        Date date = new Date();
        QueryWrapper<OnlineStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().lt(OnlineStock::getStartDate, date)
                .gt(OnlineStock::getEndDate, date)
                .gt(OnlineStock::getOnlineNumber, 0);
        List<OnlineStock> list = list(queryWrapper);
        dictionary(list);
        return list;
    }

    /**
     * 拼接条件
     *
     * @param onlineStock onlineStock
     * @return OnlineStock
     */
    public QueryWrapper<OnlineStock> getQueryWrapper(OnlineStock onlineStock) {
        QueryWrapper<OnlineStock> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(onlineStock.getCommodityName())) {
            List<Commodity> commodityList = commodityService.findByName(onlineStock.getCommodityName());
            if (commodityList.size() == 0) {
                throw new ServiceException("不存在该商品");
            }
            queryWrapper.lambda().eq(OnlineStock::getCommodityId, commodityList.get(0).getId());
        }
        if (onlineStock.getTabName() != null) {
            queryWrapper.lambda().eq(OnlineStock::getTabName, onlineStock.getTabName());
        }
        if (onlineStock.getStartDate() != null) {
            queryWrapper.lambda().le(OnlineStock::getStartDate, onlineStock.getEndDate());
        }
        if (onlineStock.getEndDate() != null) {
            queryWrapper.lambda().gt(OnlineStock::getStartDate, onlineStock.getStartDate());
        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param onlineStockList 对象集合
     */
    public void dictionary(List<OnlineStock> onlineStockList) {
        for (OnlineStock onlineStock : onlineStockList) {
            dictionary(onlineStock);
        }
    }

    public void dictionary(OnlineStock onlineStock) {
        if (onlineStock != null) {
            if (onlineStock.getTabName() != null) {
                onlineStock.setTabShowName(dictService.findByTypeCode(DictUtil.TAB_NAME, onlineStock.getTabName() + ""));
            }
            if (onlineStock.getCommodityId() != null) {
                Commodity commodity = commodityService.getById(onlineStock.getCommodityId());
                onlineStock.setCommodityName(commodity.getName());
                onlineStock.setPicUrls(commodity.getPicUrls());
                onlineStock.setRemark(commodity.getRemark());
                onlineStock.setTypeName(dictService.findByTypeCode(DictUtil.CATEGORY, commodity.getTypeName()));
            }
        }
    }

}
